package cn.wolfcode.p2p.base.service.impl;
import cn.wolfcode.p2p.Exception.ErrorCode;
import cn.wolfcode.p2p.base.domain.AuditBaseDomain;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.base.domain.UserInfo;
import cn.wolfcode.p2p.base.domain.VideoAuth;
import cn.wolfcode.p2p.base.mapper.VideoAuthMapper;
import cn.wolfcode.p2p.base.query.AuthQueryObject;
import cn.wolfcode.p2p.base.service.IUserInfoService;
import cn.wolfcode.p2p.base.service.IVideoAuthService;
import cn.wolfcode.p2p.util.AssertUtil;
import cn.wolfcode.p2p.util.BitStatesUtils;
import cn.wolfcode.p2p.util.PageResult;
import cn.wolfcode.p2p.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class VideoAuthServiceImpl implements IVideoAuthService {

    @Autowired
    private IUserInfoService userInfoService;

    @Autowired
    private VideoAuthMapper videoAuthMapper;

    @Override
    public void order(VideoAuth videoAuth) {
        //检验参数
        //检查是否已经通过了视频认证
        LoginInfo loginInfo = UserContext.getLoginInfo();
        UserInfo userInfo = userInfoService.getUserInfo(loginInfo.getId());
        AssertUtil.instance().isFalse(userInfo.hasVedioAuth(), ErrorCode.ALREADY_AUTHENTICATION_SUCCESS);
        //检查是否未通过视频认证但是提交了预约申请
        AssertUtil.instance().isFalse(userInfo.getVideoAuthId() != null, ErrorCode.ALREADY_ORDER);
        //设置参数
        VideoAuth temp = new VideoAuth();
        temp.setOrderTime(videoAuth.getOrderTime());
        temp.setOrderDate(videoAuth.getOrderDate());
        temp.setApplyTime(new Date());
        temp.setAuditor(videoAuth.getAuditor());
        temp.setApplier(loginInfo);
        videoAuthMapper.insert(temp);
        //更新信息
        userInfo.setVideoAuthId(temp.getId());
        userInfoService.update(userInfo);
    }

    /**
     * 统一更新
     *
     * @param videoAuth
     */
    @Override
    public void update(VideoAuth videoAuth) {
        videoAuthMapper.updateByPrimaryKey(videoAuth);
    }

    @Override
    public VideoAuth getVideoAuth(Long videoAuthId) {
        return videoAuthMapper.selectByPrimaryKey(videoAuthId);
    }

    @Override
    public PageResult query(AuthQueryObject qo) {
       int count = videoAuthMapper.queryForCount(qo);
       if (count == 0) {
           return PageResult.empty(qo.getPageSize());
       }
       int totalPage = count % qo.getPageSize() == 0 ? count / qo.getPageSize() : count / qo.getPageSize() + 1;
       if(totalPage < qo.getCurrentPage()){
           qo.setCurrentPage(totalPage);
       }
       List<VideoAuth> listData = videoAuthMapper.queryForList(qo);
       return new PageResult(listData,count,qo.getCurrentPage(),qo.getPageSize());

    }

    @Override
    public void audit(VideoAuth videoAuth) {
        //参数校验
        VideoAuth temp = videoAuthMapper.selectByPrimaryKey(videoAuth.getId());
        //判断状态
        //判断是否处在审核状态
        AssertUtil.instance().isFalse(temp.getState() != AuditBaseDomain.AUTH_WAIT,ErrorCode.HAVE_TO_REVIEW);
        UserInfo userInfo = userInfoService.getUserInfo(temp.getApplier().getId());
        //设置审核信息
        temp.setRemark(videoAuth.getRemark());
        temp.setAuditTime(new Date());
        //修改视频认证对象的状态
        temp.setState(videoAuth.getState());
        update(temp);
        //根据审核结果做实际业务
        if(videoAuth.getState() == AuditBaseDomain.AUTH_SUCCESS){
            //将申请人的userInfo中的位状态加上视频认证的位状态
            userInfo.addBitState(BitStatesUtils.OP_VEDIO_AUTH);
        }
        if(videoAuth.getState() == AuditBaseDomain.AUTH_REFUSE){
            //将申请人的视频认证对象的id设置为null
            userInfo.setVideoAuthId(null);
        }
        //更新信息
        userInfoService.update(userInfo);
    }

    @Override
    public void cancel(Long id) {
        //获取申请人的id
        LoginInfo applier = UserContext.getLoginInfo();
        VideoAuth videoAuth = videoAuthMapper.selectByPrimaryKey(id);
        UserInfo userInfo = userInfoService.getUserInfo(applier.getId());
        //将视频认证对象的状态设为用户取消
        videoAuth.setState(AuditBaseDomain.AUTH_CANCEL);
        userInfo.setVideoAuthId(null);
        //更新数据
        videoAuthMapper.updateByPrimaryKey(videoAuth);
        userInfoService.update(userInfo);
    }


}
